home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / comm / tcp / HyperMail102.lha / HyperMail / libcgi / form_ent.c < prev    next >
C/C++ Source or Header  |  1995-06-18  |  5KB  |  223 lines

  1. /*
  2.  * This file is part of the LIBCGI library
  3.  *
  4.  * Copyright 1994 by Enterprise Integration Technologies Corporation
  5.  *
  6.  * This is freeware with commercialization rights reserved; see the
  7.  * LICENSE included in the distribution for specifics.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include "cgi.h"
  12.  
  13. #define LF 10
  14. #define CR 13
  15.  
  16. #define STARTSIZE 8
  17.  
  18. form_entry *get_form_entries(ci)
  19.      cgi_info *ci;
  20. {
  21.   form_entry *get_fes_from_stream(), *get_fes_from_string();
  22.  
  23.   if (ci && ci->request_method && !strncasecmp(ci->request_method, "POST", 4) &&
  24.       ci->content_type &&
  25.       !strncasecmp(ci->content_type, "application/x-www-form-urlencoded", 33))
  26.     return get_fes_from_stream(ci->content_length, stdin);
  27.   else if (ci && ci->request_method && !strncasecmp(ci->request_method, "GET", 3))
  28.     return get_fes_from_string(ci->query_string);
  29.   else
  30.     return NULL;
  31. }
  32.  
  33. void free_form_entries(fe)
  34.      form_entry *fe;
  35.  {
  36.   form_entry *tempfe;
  37.   while (fe) {
  38.     if (fe->name) free(fe->name);
  39.     if (fe->val) free(fe->val);
  40.     tempfe = fe->next;
  41.     free(fe);
  42.     fe = tempfe;
  43.   }
  44. }
  45.  
  46. char *parmval(fe, s)
  47.      form_entry *fe;
  48.      char *s;
  49.  {
  50.   while (fe) {
  51.     if (!strcasecmp(fe->name, s)) return fe->val;
  52.     else fe = fe->next;
  53.   }
  54.   return NULL;
  55. }
  56.  
  57. form_entry *get_fes_from_string(s)
  58.      char *s;
  59.  {
  60.   form_entry *fe;
  61.   int asize;
  62.   int i;
  63.   char *malloc(), *realloc();
  64.   unsigned char dd2c();
  65.  
  66.   if (s == NULL) return NULL;
  67.   while (isspace(*s) || *s == '&') s++; /* some cases that shouldn't happen */
  68.   if (*s == '\0') return NULL;
  69.   fe = (form_entry *) malloc(sizeof(form_entry));
  70.   if (fe == NULL) return NULL;
  71.   fe->name = malloc((asize=STARTSIZE*sizeof(char)));
  72.   if (fe->name == NULL) {
  73.     free(fe);
  74.     return NULL;
  75.   }
  76.   /* get form field name */
  77.   for (i=0; *s && *s != '&' && *s != '='; s++,i++) {
  78.     switch (*s) {
  79.     case '+':
  80.       fe->name[i] = ' ';
  81.       break;
  82.     case '%':
  83.       fe->name[i] = dd2c(s[1], s[2]);
  84.       s += 2;
  85.       break;
  86.     default:
  87.       fe->name[i] = *s;
  88.     }
  89.     if (i+1 >= asize) { /* try to double the buffer */
  90.       fe->name = realloc(fe->name, (asize*=2));
  91.       if (fe->name == NULL) return NULL;
  92.     }
  93.   }
  94.   fe->name[i] = '\0';
  95.   switch (*s++) {
  96.   case '&':
  97.     fe->val = NULL;
  98.     break;
  99.   case '=':
  100.     fe->val = malloc((asize=STARTSIZE*sizeof(char)));
  101.     if (fe->val == NULL) break;
  102.     for (i=0; *s && *s != '&'; s++,i++) {
  103.       switch (*s) {
  104.       case '+':
  105.     fe->val[i] = ' ';
  106.     break;
  107.       case '%':
  108.     fe->val[i] = dd2c(s[1], s[2]);
  109.     s += 2;
  110.     break;
  111.       default:
  112.     fe->val[i] = *s;
  113.       }
  114.       if (i+1 >= asize) { /* try to double the buffer */
  115.     fe->val = realloc(fe->val, (asize*=2));
  116.     if (fe->val == NULL) return NULL;
  117.       }
  118.     }
  119.     fe->val[i] = '\0';
  120.     switch (*s++) {
  121.     case '&':
  122.       fe->next = get_fes_from_string(s);
  123.       break;
  124.     case '\0':
  125.     default:
  126.       fe->next = NULL;
  127.     }
  128.     break;
  129.   case '\0':
  130.   default:
  131.     fe->val = NULL;
  132.     fe->next = NULL;
  133.   }
  134.   return fe;
  135. }
  136.  
  137. #define getccl(s, l) (l-- ? getc(s) : EOF)
  138.  
  139. form_entry *get_fes_from_stream(length, stream)
  140.      int length;
  141.      FILE *stream;
  142.  {
  143.   form_entry *fe;
  144.   int asize;
  145.   int i;
  146.   int c;
  147.   char *malloc(), *realloc();
  148.   unsigned char dd2c();
  149.  
  150.   while (isspace(c=getccl(stream,length)) || c == '&');
  151.   if (c == EOF) return NULL;
  152.   fe = (form_entry *) malloc(sizeof(form_entry));
  153.   if (fe == NULL) return NULL;
  154.   fe->name = malloc((asize=STARTSIZE*sizeof(char)));
  155.   if (fe->name == NULL) {
  156.     free(fe);
  157.     return NULL;
  158.   }
  159.   /* get form field name */
  160.   for (i=0; c != EOF && c != '&' && c != '='; c=getccl(stream,length),i++) {
  161.     switch (c) {
  162.     case '+':
  163.       fe->name[i] = ' ';
  164.       break;
  165.     case '%':
  166.       fe->name[i] = dd2c(getccl(stream,length), getccl(stream,length));
  167.       break;
  168.     default:
  169.       fe->name[i] = c;
  170.     }
  171.     if (i+1 >= asize) { /* try to double the buffer */
  172.       fe->name = realloc(fe->name, (asize*=2));
  173.       if (fe->name == NULL) return NULL;
  174.     }
  175.   }
  176.   fe->name[i] = '\0';
  177.   if (c == EOF) {
  178.     fe->val = NULL;
  179.     fe->next = NULL;
  180.   }
  181.   else switch (c) {
  182.   case '&':
  183.     fe->val = NULL;
  184.     break;
  185.   case '=':
  186.     fe->val = malloc((asize=STARTSIZE*sizeof(char)));
  187.     for (i=0, c=getccl(stream,length); c != EOF && c != '&'; c=getccl(stream,length),i++) {
  188.       switch (c) {
  189.       case '+':
  190.     fe->val[i] = ' ';
  191.     break;
  192.       case '%':
  193.     fe->val[i] = dd2c(getccl(stream,length), getccl(stream,length));
  194.     break;
  195.       default:
  196.     fe->val[i] = c;
  197.       }
  198.       if (i+1 >= asize) { /* try to double the buffer */
  199.     fe->val = realloc(fe->val, (asize*=2));
  200.     if (fe->val == NULL) return NULL;
  201.       }
  202.     }
  203.     fe->val[i] = '\0';
  204.     if (c == '&') {
  205.       fe->next = get_fes_from_stream(length, stream);
  206.     }
  207.     else fe->next = NULL;
  208.   }
  209.   return fe;
  210. }
  211.  
  212. unsigned char dd2c(d1, d2)
  213.      char d1;
  214.      char d2;
  215.  {
  216.     register unsigned char digit;
  217.  
  218.     digit = (d1 >= 'A' ? ((d1 & 0xdf) - 'A')+10 : (d1 - '0'));
  219.     digit *= 16;
  220.     digit += (d2 >= 'A' ? ((d2 & 0xdf) - 'A')+10 : (d2 - '0'));
  221.     return(digit);
  222. }
  223.